﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using MyHealthByDesignWebsite.Models;
using System.Net.Mail;

namespace MyHealthByDesignWebsite.Controllers
{

    //Description:      
    //Class HomeController acts as a 'default'/'skeleton' controller only.
    //Additional controllers should be created to handle significant functionality.
    //Created:  Oct 2011
    //Programmer:  Jon Barnard
    //Modifications: Kinosha Wimalaratne (Dec 2011- add user login )


    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Title = "ViewBag.Title";
            ViewBag.Message = "HomeController.Index()";
            return View();
        }

        //
        // POST: 
        //pre-condition: User has to enter username and password
        //post-condition: Check for valid user input and redirect to my health home
        [HttpPost]
        public ActionResult Index(LogOnModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    if (Membership.ValidateUser(model.UserName, model.Password))
                    {
                        FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                        if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1
                            && returnUrl.StartsWith("/") && !returnUrl.StartsWith("//")
                            && !returnUrl.StartsWith("/\\"))
                        {
                            return Redirect(returnUrl);
                        }
                        else
                        {
                            FullDataSetDataContext db = new FullDataSetDataContext();
                            User usr = db.Users.Single(u => u.U_email == model.UserName);
                            //add userid to cookie
                            HttpCookie userIDCookie = new HttpCookie("UserID", Convert.ToString(usr.U_userID));
                            userIDCookie.Expires = DateTime.Now.AddYears(1);
                            Response.Cookies.Add(userIDCookie);

                            //if user has not yet filled out initial survey(s)
                            if (Roles.IsUserInRole(User.Identity.Name, "HealthCheckComplete"))
                                return RedirectToAction("MyHealth", "Home");
                            else
                                return RedirectToAction("Index", "MyHealthMeasurements");
                        }
                    }
                    else
                    {
                        ModelState.AddModelError
                            ("", "The user name or password provided is incorrect.");
                    }
                }
                catch (Exception e)
                {
                    ModelState.AddModelError
                        ("", "Database Error. Database could not be contacted");
                }
            }//end if model state is valid

            // If we got this far, something failed, redisplay form
            return View(model);
        }
        

        public ActionResult MyHealth()
        {
            ViewBag.Message = "MyHealth";
            return View();
        }

        public ActionResult Fitness()
        {
            ViewBag.Message = "Fitness";
            return View();
        }
        
        public ActionResult DietAndNutrition()
        {
            ViewBag.Message = "DietAndNutrition";
            return View();
        }
        
        public ActionResult StressManagement()
        {
            ViewBag.Message = "StressManagement";
            return View();
        }
        
        public ActionResult CorporateServices()
        {
            ViewBag.Message = "CorporateServices";
            return View();
        }
        
        public ActionResult About()
        {
            ViewBag.Message = "About";
            return View();
        }
    }
}